package com.itboye.pondteam.adapter;

import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.itboye.pondteam.R;
import com.itboye.pondteam.bean.WeiShiModel;
import com.itboye.pondteam.custom.MyCircleTextView;
import com.itboye.pondteam.custom.swiprecyclerview.SwipeItemLayout;
import com.itboye.pondteam.custom.swiprecyclerview.Type;
import com.itboye.pondteam.interfaces.IRecyclerviewclicklistener;

import java.util.ArrayList;

/**
 * Created by Mr.w on 2017/5/13.
 */

public class WeiShiAdapter extends RecyclerView.Adapter<WeiShiAdapter.SimpleViewHolder> {

    private IRecyclerviewclicklistener mItemTouchListener;
    private ArrayList<WeiShiModel> mData;

    public WeiShiAdapter(ArrayList<WeiShiModel> data, IRecyclerviewclicklistener itemTouchListener) {
        this.mData = data;
        this.mItemTouchListener = itemTouchListener;
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getType();
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        @LayoutRes
        int layout;

        switch (viewType) {
            case Type.LEFT_MENU:
                layout = R.layout.item_left_menu;
                break;
            case Type.RIGHT_MENU:
                layout = R.layout.item_right_menu;
                break;
            case Type.LEFT_AND_RIGHT_MENU:
                layout = R.layout.item_left_and_right_menu;
                break;
            case Type.LEFT_LONG_MENU:
                layout = R.layout.item_left_long_menu;
                break;
            case Type.RIGHT_LONG_MENU:
                layout = R.layout.item_right_long_menu;
                break;
            case Type.LEFT_AND_RIGHT_LONG_MENU:
                layout = R.layout.item_left_and_right_long_menu;
                break;
            default:
                layout = R.layout.item_left_menu;
                break;
        }
        View rootView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
        return new SimpleViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(final SimpleViewHolder holder, int position) {
        holder.mContent.setMyText("" + position);
        if (mItemTouchListener != null) {
            //jdk1.8使用
//            holder.itemView.setOnClickListener(v -> mItemTouchListener.onItemClick(holder.mContent.getText().toString()));
//
//            if (holder.mLeftMenu != null) {
//                holder.mLeftMenu.setOnClickListener(v -> {
//                    mItemTouchListener.onLeftMenuClick("left " + holder.getAdapterPosition());
//                    holder.mSwipeItemLayout.close();
//                });
//            }
//
//            if (holder.mRightMenu != null) {
//                holder.mRightMenu.setOnClickListener(v -> {
//                    mItemTouchListener.onRightMenuClick("right " + holder.getAdapterPosition());
//                    holder.mSwipeItemLayout.close();
//                });
//            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemTouchListener.onItemClick(holder.mContent.getText().toString());
                }
            });

            if (holder.mLeftMenu != null) {
                holder.mLeftMenu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemTouchListener.onLeftMenuClick("left " + holder.getAdapterPosition());
                        holder.mSwipeItemLayout.close();
                    }
                });
            }

            if (holder.mRightMenu != null) {
                holder.mRightMenu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemTouchListener.onRightMenuClick("right " + holder.getAdapterPosition());
                        holder.mSwipeItemLayout.close();
                    }
                });
            }
        }
    }

    public static class SimpleViewHolder extends RecyclerView.ViewHolder {

        private final View mLeftMenu;
        private final View mRightMenu;
        private final MyCircleTextView mContent;
        private final SwipeItemLayout mSwipeItemLayout;

        SimpleViewHolder(View itemView) {
            super(itemView);
            mSwipeItemLayout = (SwipeItemLayout) itemView.findViewById(R.id.swipe_layout);
            mContent = (MyCircleTextView) itemView.findViewById(R.id.tv_content);
            mLeftMenu = itemView.findViewById(R.id.left_menu);
            mRightMenu = itemView.findViewById(R.id.right_menu);
        }
    }
}
